using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;  
using System.Net.Sockets;
using System.IO;
using System.Net;

namespace Client
{
    public partial class Form1 : Form
    {
        private NetworkStream output; // stream for receiving data           
        private BinaryWriter writer; // facilitates writing to the stream    
        private BinaryReader reader; // facilitates reading from the stream  
        private Thread readThread; // Thread for processing incoming messages
        private string message = "";
        //private string IPService = "10.0.83.35";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Message mess = new Message();
            mess.Header = txtHeader.Text;//"123456";
            mess.TypeIndentifier = txtTypeIndentifier.Text;//"1234";
            mess.PrimaryBitMap = txtPrimaryBitMap.Text;//"1234567890123456";
            mess.ProcessingCode = txtProcessingCode.Text;//"123456";
            mess.ResponseCode = txtResponseCode.Text; // "12";           
            mess.UserID = txtUserID.Text;
            mess.OTPNumber = txtOTP.Text;
            //MessageBox.Show(mess.Content);
            try
            {
                if (mess.Content.Length >= 56)
                {
                    richTextBox1.Clear();
                    richTextBox1.Text = mess.Content;
                    writer.Write(mess.Content);
                }
                else
                {
                    MessageBox.Show("Message is invalid!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Disconnected, You try to connect again!! ");
                //throw;
            }
            
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {       
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {            
            System.Environment.Exit(System.Environment.ExitCode);
        }

        // connect to server and display server-generated text
        public void RunClient()
        {
            TcpClient client;
            // instantiate TcpClient for sending data to server
            try
            {
                //MessageBox.Show("Attempting connection\r\n","Client");

                // Step 1: create TcpClient and connect to server                
                client = new TcpClient();
                client.Connect(GetIPAddress(), 1917);            

                // Step 2: get NetworkStream associated with TcpClient
                output = client.GetStream();                          

                // create objects for writing and reading across stream
                writer = new BinaryWriter( output );                   
                reader = new BinaryReader( output );

                do
                {                    
                    // Step 3: processing phase
                    try
                    {
                        // read message from server                                              
                        message = reader.ReadString();                        
                        Message mess = new  Message(message);                        
                        if(Int32.Parse(mess.ResponseCode) == OTPDB_DEFINE.DB_ERR_SUCCESS)
                        {
                            MessageBox.Show("Login successful!!!!!   " + message + "     Response code is : " + mess.ResponseCode);
                        }
                        else
                        {
                            MessageBox.Show("Login fault!!!!    " + message + "     Response code is : " + mess.ResponseCode);
                        }
                    } // end try
                    catch ( Exception e)
                    {
                        //MessageBox.Show("You try to connect again!!");
                        break;
                    } // end catch
                } while (client.Connected);
                
                // Step 4: close connection
                writer.Close();
                reader.Close();
                output.Close();
                client.Close();
            } // end try
            catch ( Exception error )
            {
                // handle exception if error in establishing connection
                MessageBox.Show( error.ToString(), "Connection Error",MessageBoxButtons.OK, MessageBoxIcon.Error );
                System.Environment.Exit( System.Environment.ExitCode );
            } // end catch
        }
        private string GetIPAddress()
        {            
            return txtIPServer.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                   readThread = new Thread(new ThreadStart(RunClient));
                    readThread.Start();
                
                MessageBox.Show("Connect successfully!");
            }
            catch (Exception)
            {
                MessageBox.Show("Connect fault!");
                //throw;
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}